<template>
  <div>
    <com-card>
      <div slot="com_title"  class="title_con">
        <span>商品添加</span>
      </div>
       <div slot="com_content">
        <el-form
          ref="form"
          :rules="rules"
          :model="goods"
          label-width="80px"
          size="small"
        >
          <el-form-item label="商品名称" prop="name">
            <el-input v-model="goods.name" placeholder="商品名称"></el-input>
          </el-form-item>
          <el-form-item label="商品分类" prop="category">
            <el-select v-model="goods.category" placeholder="请选择商品分类">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="商品价格" prop="price">
            <el-input-number
              v-model="goods.price"
              :precision="2"
              :step="0.1"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="商品图片" prop="imgUrl">
            <el-upload
              v-model="goods.imgUrl"
              class="avatar-uploader"
              action="http://localhost:5000/goods/goods_img_upload"
              :show-file-list="false"
              :http-request="upload"
              :before-upload="beforeAvatarUpload"
            >
              <el-image
                v-if="goods.imgUrl"
                :src="
                  'http://localhost:5000/upload/imgs/goods_img/' + goods.imgUrl
                "
                class="avatar"
              ></el-image>
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="商品描述" prop="goodsDesc">
            <el-input type="textarea" v-model="goods.goodsDesc"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">添加商品</el-button>
          </el-form-item>
        </el-form>
      </div>
    </com-card>
  </div>
</template>

<script>
import ComCard from "../../components/comcard/index.vue"
import { api_categories, api_addgoods, api_upload } from "../../api/goods";
export default {
  data() {
    return {
      num: 0,
      goods: {
        name: "",
        category: "",
        price: 0,
        imgUrl: "",
        goodsDesc: "",
      },
      options: [],
      rules: {
        name: { required: true, message: "请输入商品名称", trigger: "blur" },
        category: {
          required: true,
          message: "请选择商品分类",
          trigger: "blur",
        },
        imgUrl: { required: true, message: "请上传商品图片", trigger: "blur" },
        goodsDesc: {
          required: true,
          message: "请输入商品描述",
          trigger: "blur",
        },
      },
    };
  },
  components: {
    ComCard,
  },
  methods: {
    async getCategories() {
      const { categories } = await api_categories();
      this.options = categories.map((i) => {
        i.value = i.cateName;
        i.label = i.cateName;
        return i;
      });
    },
    beforeAvatarUpload(file) {
      const isJPG =
        file.type === "image/jpeg" ||
        file.type === "image/jpg" ||
        file.type === "image/png";
      if (!isJPG) {
        this.$message.error("上传图片只能是 jpg、jpeg、png 格式!");
      }
      return isJPG;
    },

    async upload(e) {
      const { code, msg, imgUrl } = await api_upload(e.file);
      if (code == 0) {
        this.goods.imgUrl = imgUrl;
      } else {
        this.$message.error(msg);
      }
      this.$forceUpdate();
    },
    onSubmit() {
      this.$refs.form.validate(async (v) => {
        if (v) {
          const { code, msg } = await api_addgoods(this.goods);
          if (code == 0) {
            this.$message.success(msg);
          } else {
            this.$message.error(msg);
          }
        } else {
          return false;
        }
      });
    },
  },
  mounted() {
    this.getCategories();
  },
};
</script>

<style lang="less" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.el-input {
  width: 450px;
}
</style>